package com.奇技淫巧;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.nio.charset.Charset;

public class CSV文件读取 {

    public static void main(String[] args) throws IOException {
        //FileInputStream inputStream = new FileInputStream("C:\\Users\\admin\\Desktop\\20240807_6666000152951276_settleuser_1.csv");
        //Workbook wb = WorkbookFactory.create(inputStream);
        //Sheet sheet = wb.getSheetAt(0);
        //Sheet sheet = wb.cloneSheet(0);

        //FileInputStream xlsx = new FileInputStream("C:\\Users\\admin\\Desktop\\20240807_6666000152951276_settleuser_1.xlsx");
        Workbook workbook = new XSSFWorkbook();//WorkbookFactory.create(xlsx);
        Sheet sheet = workbook.createSheet("对账单");

        String fileUrl = "C:\\Users\\admin\\Desktop\\20240807_6666000152951276_settleuser_1.csv";
        //FileReader fileReader = new FileReader(fileUrl);//, Charset.forName("UTF-8")
        //BufferedReader reader = new BufferedReader(fileReader);
        //InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(fileUrl),Charset.forName("UTF-8"));
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(fileUrl),Charset.forName("GBK"));
        BufferedReader reader = new BufferedReader(inputStreamReader);
        String line;

        int rowNum = 0;
        while ((line = reader.readLine())!=null){
            Row row = sheet.createRow(rowNum++);
            String[] values = line.split(",");
            for(int i=0; i<values.length; i++){
                Cell cell = row.createCell(i);
                cell.setCellValue(substring(values[i]));
            }
        }
        /*FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\admin\\Desktop\\20240807_6666000152951276_settleuser_2.xlsx");
        workbook.write(fileOutputStream);
        FileInputStream xlsx = new FileInputStream(fileOutputStream.getFD());
        upload(xlsx);
        workbook.close();
        fileOutputStream.close();*/

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        ByteArrayInputStream cloneInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        upload(cloneInputStream);
        workbook.close();
        byteArrayOutputStream.close();

    }

    private static String substring(String value){
        if(value.startsWith("=\"")){
            return value.substring(2,value.length()-1);
        }
        return value;
    }

    private static String upload(InputStream inputStream){
        String bucketName = "datacenter-export";
        OSSClient ossClient = new OSSClient("oss-cn-beijing.aliyuncs.com",
                new DefaultCredentialProvider("LTAI5tRV4aab6z3tQjnPWxin", "7sNDfTGYuVG16WAauNLcxYpmFTHNLm"),
                new ClientConfiguration());
        ossClient.putObject(bucketName, "disdr" ,inputStream);
        return "";
    }

}
